package com.qqt.csr.archive.service;

import com.qqt.csr.archive.entity.reader.SmartMessage;
import com.qqt.csr.archive.mapper.SmartMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/11/21 10:08
 * @Description: 智能客服 消息
 **/
@Slf4j
@Service
public class SmartMessageService {

    @Autowired
    private SmartMessageMapper smartMessageMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据消息ID获取消息
     * @param id {@link String} 消息id
     * @return {@link SmartMessage} 智能客服消息
     */
    public SmartMessage getById(String id) {
        return smartMessageMapper.findById(id).orElse(null);
    }

    /**
     * 根据会话ID获取消息列表
     * @param conversationId {@link String} 会话ID
     * @return {@link List<SmartMessage>} 消息列表
     */
    public List<SmartMessage> getByConversationId(String conversationId) {
        Query query = Query.query(Criteria.where("sessionId").is(conversationId))
                .with(Sort.by(Sort.Direction.ASC, "createTime"));
        return mongoTemplate.find(query, SmartMessage.class);
    }

}
